package com.company.dcm.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.dcm.constant.RoleConstant;
import com.company.dcm.entity.Permission;
import com.company.dcm.entity.User;
import com.company.dcm.entity.WorkShop;
import com.company.dcm.mapper.FactoryMapper;
import com.company.dcm.mapper.PermissionMapper;
import com.company.dcm.mapper.UserMapper;
import com.company.dcm.mapper.WorkShopMapper;
import com.company.dcm.entity.Factory;
import com.company.dcm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private WorkShopMapper workShopMapper;
    @Autowired
    private FactoryMapper factoryMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Long> getWorkShopIdsByUserId(Long userId){
        List<Long> result=new ArrayList<>();
        User user = userMapper.selectById(userId);
        //boss返回全部
        if(StpUtil.getRoleList(user.getUserName()).contains(RoleConstant.BOSS)){
            List<WorkShop> workShopList = workShopMapper.selectList(null);
            for (WorkShop workShop : workShopList){
                result.add(workShop.getId());
            }
            return result;
        }

        List<Permission> permissions = permissionMapper.selectList(new LambdaQueryWrapper<Permission>().eq(Permission::getUserId,userId));
        for(Permission permission:permissions){
            if(permission.getFactoryId()!=null){
                List<WorkShop> workShops = workShopMapper.selectList(new LambdaQueryWrapper<WorkShop>().eq(WorkShop::getFactoryId,permission.getFactoryId()));
                for (WorkShop workShop:workShops) {
                    if(workShop.getId()!=null)
                        result.add(workShop.getId());
                }
            }else if(permission.getId()!=null){
                result.add(permission.getId());
            }
        }
        return result;
    }

    @Override
    public List<Long> getFactoryIdsByUserId(Long userId) {
        List<Long> result=new ArrayList<>();
        User user = userMapper.selectById(userId);
        //boss返回全部
        if(StpUtil.getRoleList(user.getUserName()).contains(RoleConstant.BOSS)){
            List<Factory> factoryList = factoryMapper.selectList(null);
            for (Factory factory : factoryList){
                result.add(factory.getFactoryId());
            }
            return result;
        }

        List<Permission> permissions = permissionMapper.selectList(new LambdaQueryWrapper<Permission>().eq(Permission::getUserId,userId));
        for (Permission permission:permissions) {
            if(permission.getFactoryId()!=null){
                result.add(permission.getFactoryId());
            }
        }
        return result;
    }

    @Override
    @Transactional
    public boolean deleteWorkShop(Permission permission, User user) {
        if(permission==null|user==null)return false;

        permissionMapper.delete(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionId,permission.getPermissionId()));
        List<Permission> permissions = permissionMapper.selectList(new LambdaQueryWrapper<Permission>().eq(Permission::getUserId,user.getUserId()));
        for (Permission p:permissions) {
            if(p.getId()!=null){
                return true;
            }
        }
        user.setRoleId(0);
        return userMapper.updateById(user)>0;
    }

    @Override
    @Transactional
    public boolean deleteFactory(Permission permission, User user) {
        if(permission==null|user==null)return false;

        permissionMapper.delete(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionId,permission.getPermissionId()));
        List<Permission> permissions = permissionMapper.selectList(new LambdaQueryWrapper<Permission>().eq(Permission::getUserId,user.getUserId()));
        for (Permission p:permissions) {
            if(p.getFactoryId()!=null){
                return true;
            }
        }
        user.setRoleId(0);
        return userMapper.updateById(user)>0;
    }
}
